<template>
  <div class="container">
       <a-page-header
          title="登录"
        />
  <a-form
    id="components-form-demo-normal-login"
    :form="form"
    class="login-form"
    @submit="handleSubmit"
  >
    <a-form-item>
      <a-input
        v-decorator="[
          'username',
          { rules: [{ required: true, message: 'Please input your username!' }] },
        ]"
        placeholder="Username or Account Address"
      >
        <a-icon slot="prefix" type="user" style="color: rgba(0,0,0,.25)" />
      </a-input>
    </a-form-item>
    <a-form-item>
      <a-input
        v-decorator="[
          'password',
          { rules: [{ required: true, message: 'Please input your Password!' }] },
        ]"
        type="password"
        placeholder="Password"
      >
        <a-icon slot="prefix" type="lock" style="color: rgba(0,0,0,.25)" />
      </a-input>
    </a-form-item>
    <a-form-item>
      <a-checkbox
        v-decorator="[
          'remember',
          {
            valuePropName: 'checked',
            initialValue: true,
          },
        ]"
      >
        Remember me
      </a-checkbox>
      <a class="login-form-forgot" href="">
        Forgot password
      </a>
      <a-button type="primary" html-type="submit" class="login-form-button">
        Log in
      </a-button>
      Or
      <a-button type="link" @click="toRegister">
        register now!
      </a-button>
     Or
      <a-button type="link" @click="toRegisterSuperUser">
        register super user!
      </a-button>
    </a-form-item>
  </a-form>
  </div>
</template>


<script>
import {login} from "@/api/user/user.js"

export default {
  beforeCreate() {
    this.form = this.$form.createForm(this, { name: 'normal_login' });
  },
  data() {
      return {
      }
  },
  methods: {
    handleSubmit(e) {
      e.preventDefault();
      this.form.validateFields((err, values) => {
        if (!err) {
          console.log('Received values of form: ', values);
          // var param = {}
          // param['username'] = values.username
          // param['password'] = values.password
          // console.log(param)
          login(values)
          .then(data=>{
            console.log(data)
            if(data.code != 200){
              this.$message.error("登录失败：用户未激活或用户名密码错误")
              return
            }
            // 成功处理
            this.$message.success("登录成功，欢迎：" + data.data.username)
            localStorage.setItem("currentLoginUser", JSON.stringify(data.data))
            this.$router.push({name:'home'})
            location.reload()
          })
          .catch(err=>{
            this.$message.error("登录错误")
          })
        }
      });
    },
    toRegister(){
        this.$router.push({name:"register"})
    },

    toRegisterSuperUser(){
        this.$router.push({name:"registerSuperUser"})
    }
  },
}
</script>

<style lang="less" scoped>
.container{
    margin: auto;
    width: 400px;
    height: 700px;
    padding-top: 40px;
}

#components-form-demo-normal-login .login-form {
  max-width: 300px;
}
#components-form-demo-normal-login .login-form-forgot {
  float: right;
}
#components-form-demo-normal-login .login-form-button {
  width: 100%;
}
</style>